//import airplaneNode from "./airplaneNode";
const { ccclass, property } = cc._decorator;

@ccclass
export default class airplaneMove extends cc.Component {
    airplaneNode: cc.Node = null;
    airplaneNodeTs: cc.Component = null;
    onLoad() {
        this.node.on("touchmove", this.touchmove, this)
        this.node.on("touchcancel", this.touchcancel, this)
        this.node.on("touchend", this.touchend, this)
        this.airplaneNode = cc.find('Canvas/飞机')
        this.airplaneNodeTs = this.airplaneNode.getComponent('airplaneNode');
        console.log(this.airplaneNodeTs)
    }
    touchmove(e: cc.Event.EventTouch) {
        //将触摸点的世界坐标转为本地坐标。
        let positionWorld: cc.Vec2 = this.node.parent.convertToNodeSpaceAR(e.getLocation());
        this.node.setPosition(positionWorld);
        //设置归一向量 
        let direction: cc.Vec2 = positionWorld.normalize();
        let max: number = 100;
        let instanceMax: number = cc.Vec2.distance(positionWorld, cc.v2(0, 0));
        //设置移动范围
        if (instanceMax > max) {
            positionWorld.x = direction.x * max;
            positionWorld.y = direction.y * max;
        }
        this.airplaneNodeTs.direction = direction;
        this.node.setPosition(positionWorld);
    }
    touchcancel() {

        this.node.setPosition(cc.v2(0, 0));
        this.airplaneNodeTs.direction = null;
    }
    touchend() {
        this.node.setPosition(cc.v2(0, 0));
        this.airplaneNodeTs.direction = null;
    }
    start() {

    }

    // update (dt) {}
}



